package com.huaruan.youdi.utils;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.huaruan.youdi.base.BaseApplication;

public class DeviceUtils {
    public static Display getDefaultDisplay() {
        return ((WindowManager)  BaseApplication.getInstance().getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    }

    public static int dp2px(float dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,  BaseApplication.getInstance().getContext().getResources().getDisplayMetrics());
    }

    public static int px2dp(float px) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, px,  BaseApplication.getInstance().getContext().getResources().getDisplayMetrics());
    }

    public static boolean isTablet(Context c) {
        return (c.getResources().getConfiguration().screenLayout
                & Configuration.SCREENLAYOUT_SIZE_MASK)
                >= Configuration.SCREENLAYOUT_SIZE_LARGE;
    }

    public static float getScaledDensity() {
        return  BaseApplication.getInstance().getContext().getResources().getDisplayMetrics().scaledDensity;
    }

    public static int[] getScreen() {
        return new int[]{getDefaultDisplay().getWidth(), getDefaultDisplay().getHeight()};
    }

    public static int getActionBarSize() {
        int actionBarHeight = 0;
        TypedValue tv = new TypedValue();
        if ( BaseApplication.getInstance().getContext().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
            actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,  BaseApplication.getInstance().getContext().getResources().getDisplayMetrics());
        }
        return actionBarHeight;
    }

    /**
     * 获取状态栏高度
     *
     * @return
     */
    public static int getStatusBarHeight() {
        int result = 0;
        int resourceId =  BaseApplication.getInstance().getContext().getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = BaseApplication.getInstance().getContext().getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    /**
     * 沉浸式状态栏设置距离状态栏间距（按需设置，非必需），这里的使用场景主要是一个页面有多个fragment（类似viewpager)切换的情况
     * 其他方式：
     * 1、在activity的layout中设置fitSystemWindows为true
     * 2、通过代码设置
     * ViewGroup mContentView = (ViewGroup) IGetActivity().findViewById(Window.ID_ANDROID_CONTENT);
     * View mChildView = mContentView.getChildAt(0);
     * if (mChildView != null) {
     * 注意不是设置 ContentView 的 FitsSystemWindows, 而是设置 ContentView 的第一个子 View . 预留出系统 View 的空间.
     * mChildView.setFitsSystemWindows(true);
     * }
     *
     * @param anyView
     * @param isSetWindowsBackgroundBlack 窗口默认颜色f7f7f7f（灰白），视觉效果不好，这里主要用于改变状态栏背景色，
     *                                    可根据情况设置，默认不用设置（true）,该属性必需在setContentView方法后调用才有效
     */
    public static void setTranslucentBar(View anyView, boolean isSetWindowsBackgroundBlack) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (anyView == null)
                return;
            ViewGroup.LayoutParams layoutParams = anyView.getLayoutParams();
            if (layoutParams instanceof RelativeLayout.LayoutParams)
                ((RelativeLayout.LayoutParams) layoutParams).topMargin = DeviceUtils.getStatusBarHeight();
            else if (layoutParams instanceof LinearLayout.LayoutParams)
                ((LinearLayout.LayoutParams) layoutParams).topMargin = DeviceUtils.getStatusBarHeight();
            else if (layoutParams instanceof FrameLayout.LayoutParams)
                ((FrameLayout.LayoutParams) layoutParams).topMargin = DeviceUtils.getStatusBarHeight();
            else if (layoutParams instanceof AppBarLayout.LayoutParams)
                ((AppBarLayout.LayoutParams) layoutParams).topMargin = DeviceUtils.getStatusBarHeight();
            else if (layoutParams instanceof CoordinatorLayout.LayoutParams)
                ((CoordinatorLayout.LayoutParams) layoutParams).topMargin = DeviceUtils.getStatusBarHeight();
            if (isSetWindowsBackgroundBlack)
                anyView.getRootView().setBackgroundColor(Color.BLACK);
        }
    }

    public static void setTranslucentBar(View toolbarParent) {
        setTranslucentBar(toolbarParent, false);
    }

    public static int getScreenWidth(Context context) {
        Resources resources = context.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        return dm.widthPixels;
    }

    public static int getScreenHeight(Context context) {
        Resources resources = context.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        return dm.heightPixels;
    }

    public static int getWidth(View view) {
        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        view.measure(w, h);
        return view.getMeasuredWidth();
    }

    public static int getHeight(View view) {
        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        view.measure(w, h);
        return view.getMeasuredHeight();
    }
}
